package com.scholarship.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.scholarship.system.entity.Student;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 学生信息Mapper接口
 */
public interface StudentMapper extends BaseMapper<Student> {

    /**
     * 查询学生列表（联表查询student和user表）
     * 
     * @param advisorId 导师ID，如果为null则获取所有学生
     * @param keyword   关键字搜索（学号/姓名）
     * @return 学生列表
     */
    @Select({ "<script>",
            "SELECT s.id, s.student_id, u.name, u.gender, s.college, s.major, s.grade, s.class_name, ",
            "s.entry_year, s.education_level, s.advisor_id ",
            "FROM student s ",
            "LEFT JOIN user u ON s.user_id = u.id ",
            "<where>",
            "<if test='advisorId != null'>",
            "s.advisor_id = #{advisorId} ",
            "</if>",
            "<if test='keyword != null and keyword != \"\"'>",
            "<if test='advisorId != null'>AND</if> (s.student_id LIKE CONCAT('%', #{keyword}, '%') OR u.name LIKE CONCAT('%', #{keyword}, '%'))",
            "</if>",
            "</where>",
            "ORDER BY s.id DESC",
            "</script>" })
    List<Map<String, Object>> selectStudentList(@Param("advisorId") Long advisorId, @Param("keyword") String keyword);
}